﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace OpenRPOSBackOffice
{
    public partial class AddPunchDialog : ChildWindow
    {
        public AdjustTimeSheetDialog atsd;

        public AddPunchDialog()
        {
            InitializeComponent();
        }

        public void init()
        {
            // Fill the role dialog...
            foreach (ServiceReferenceOpenRPOS.Role role in Globals.db.RoleRecords)
                foreach (ServiceReferenceOpenRPOS.EmployeeRole employeeRole in Globals.db.EmployeeRoleRecords)
                {
                    if (atsd.currentEmployeeID == employeeRole.col_EmployeeID && employeeRole.col_RoleID == role.col_ID)
                        cmbRole.Items.Add(role.col_Title);
                }
            if (cmbRole.Items.Count() > 0)
                cmbRole.SelectedIndex = 0;
        }

        private async void OKButton_Click(object sender, RoutedEventArgs e)
        {
            // Add the new punch
            ServiceReferenceOpenRPOS.TimeSheet ts = new ServiceReferenceOpenRPOS.TimeSheet();
            ts.bFilterEmployeeID = true;
            ts.bFilterPunchType = true;
            ts.bFilterAdjustment = true;
            ts.bFilterPunchDateTime = true;
            ts.bFilterAdjustmentByEmployeeID = true;
            if (rbIn.IsChecked == true && cmbRole.SelectedIndex>=0)
            {
                ts.bFilterRoleID = true;
                ts.col_RoleID = Globals.db.RoleRecords.Where(x => x.col_Title == (string)cmbRole.SelectedValue).First().col_ID;
            }
            ts.col_EmployeeID = atsd.currentEmployeeID;
            ts.col_PunchType = (rbIn.IsChecked == true) ? true : false;
            ts.col_Adjustment = true;
            ts.col_PunchDateTime = (System.DateTime)tpPunchTime.Value;
            ts.col_AdjustmentByEmployeeID = Globals.currentEmployee.col_ID;
            await Globals.db.AddTimeSheetObject(ts);
            // Re-initialize the caller
            atsd.init(atsd.currentEmployeeID, ts.col_PunchDateTime);
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void rbIn_Checked(object sender, RoutedEventArgs e)
        {
            // Show the role information
            cmbRole.Visibility = Visibility.Visible;
            lblRole.Visibility = Visibility.Visible;
        }

        private void rbOut_Checked(object sender, RoutedEventArgs e)
        {
            // Hide the role information
            cmbRole.Visibility = Visibility.Collapsed;
            lblRole.Visibility = Visibility.Collapsed;
        }
    }
}

